/*******************************************************************************
 * Copyright 2011-2012 Dik Grapendaal
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package sh.grapendaal.tsuushin;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Proxy class to redirect any OutputStream to a logging call.
 */
public abstract class LogStream extends OutputStream {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void write(byte[] b) throws IOException {
		String line = new String(b);
		if (!line.trim().isEmpty()) {
			this.log(line);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void write(byte[] b, int off, int len) throws IOException {
		String line = new String(b, off, len);
		if (!line.trim().isEmpty()) {
			this.log(line);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void write(int b) throws IOException {
		String line = String.valueOf((char) b);
		if (!line.trim().isEmpty()) {
			this.log(line);
		}
	}

	/**
	 * This method implements the actual logging call.
	 * 
	 * @param line
	 *            The line we received from one of the write() methods.
	 */
	protected abstract void log(String line);
}
